1476A - K-divisible Sum - CodeForces Solution


binary search constructive algorithms greedy math *1000

Please click on ads to support us..

Python Code:

import math
for _ in range(int(input())):
    n,k = map(int,input().split())
    if k==1:
        print(1)
    elif n>k:
        if n%k==0:
            print(1)
        else:
            print(2)
        
        
    elif n==k:
        print(1)
    else:
        print(math.ceil(k/n))

C++ Code:

#include<bits/stdc++.h>
using namespace std;

//Aliases
using ll = long long;
using ld = long double;
using ull = unsigned long long;

//macros
#define nline "\n"
#define MOD 1e9 + 7
#define INF 1e18
#define int long long 
#define all(x) begin(x), end(x)
#define Debug(x)   cout << #x << " = " << x << "\n" 
#define pb emplace_back

// operator overloading
template <typename T> // cin >> vector<T>
istream& operator>>(istream& istream, vector<T>& v)
{
    for (auto& it : v)
        cin >> it;
    return istream;
}
template <typename T> // cout << vector<T>
ostream &operator<<(ostream &ostream, const vector<T> &c)
{
    for (auto &it : c)
        cout << it << " ";
    return ostream;
}

//Mathematical Function
int GCD(int a, int b)
{
    while (b)
    {
        a %= b;
        swap(a, b);
    }
    return a;
}

int mod_add(int a, int b, int m=MOD)
 {
    a = a % m; 
    b = b % m; 
    return (((a + b) % m) + m) % m;
}

int mod_mul(int a, int b, int m=MOD)
 {
    a = a % m;
     b = b % m; 
     return (((a * b) % m) + m) % m;
 }

int mod_sub(int a, int b, int m=MOD)
 {
    a = a % m; 
    b = b % m;
     return (((a - b) % m) + m) % m;
 }
 // Calculating Power 
int modpow(int x, int n, int m = MOD)
{
    if (x == 0 && n == 0)
        return 0; // undefined case
    ll res = 1;
    while (n > 0)
    {
        if (n % 2)
            res = mod_mul(res,x);
        x = mod_mul(x,x);
        n /= 2;
    }
    return res;
}
// Modulo Inverse
int modinv(int x, int m = MOD)
{
    return modpow(x, m - 2, m);
}
int binpow(int a, int b) {
    if (b == 0)
        return 1;
    int res = binpow(a, b / 2);
    if (b % 2)
        return res * res * a;
    else
        return res * res;
}
/*---------------------------------------------------------------------------------*/



void shubham()
{
     
    int n,k;
    cin>>n>>k;
    int start=0;
    int end=1e9;
    int ans=0;
    while(start<=end){
        int mid=(start+end)/2;
        if(n*mid>=k){
            ans=mid;
            end=mid-1;
        }
        else{
            start=mid+1;
        }
    }
    if(ans==1&&(ans*n)%k!=0){
        cout<<ans+1<<"\n";
        return;
    }
    cout<<ans<<"\n";
}

signed main(){
   #ifndef ONLINE_JUDGE
   freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt","w",stderr);
  #endif 

     //fast io
      ios_base::sync_with_stdio(false);
    cin.tie(NULL);
     cout << setprecision(12) << fixed;
    
    int tt=1;
    cin>>tt;
   while(tt--){
    shubham();
}
}


Comments

Submit
0 Comments
More Questions

2. Add Two Numbers
515. Find Largest Value in Each Tree Row
345. Reverse Vowels of a String
628. Maximum Product of Three Numbers
1526A - Mean Inequality
1526B - I Hate 1111
1881. Maximum Value after Insertion
237. Delete Node in a Linked List
27. Remove Element
39. Combination Sum
378. Kth Smallest Element in a Sorted Matrix
162. Find Peak Element
1529A - Eshag Loves Big Arrays
19. Remove Nth Node From End of List
925. Long Pressed Name
1051. Height Checker
695. Max Area of Island
402. Remove K Digits
97. Interleaving String
543. Diameter of Binary Tree
124. Binary Tree Maximum Path Sum
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
501A - Contest
160A- Twins
752. Open the Lock
1535A - Fair Playoff
1538F - Interesting Function
1920. Build Array from Permutation
494. Target Sum
797. All Paths From Source to Target